package com.ebelter.nb.ui.activitys;



import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.btcomlib.models.bluetooth.products.nb.BondDevices;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.activity_manager.ActivityManagers;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.nb.R;
import com.ebelter.nb.model.http.HttpResponse2;
import com.ebelter.nb.model.http.request.NbNetUtils;
import com.ebelter.nb.model.http.response.NbBaseResponse;

public class FindPswActivity2 extends NBBaseActivity {

    public static final String TAG = "FindPswActivity2";


    private ImageView top_left_iv;
    private TextView title_tv;
    private TextView rightTitle_tv;
    private EditText p1;
    private EditText p2;


    String phone;
    String phoneCode;

    @Override
    protected int loadLayoutById() {
        return R.layout.activity_set_psw;
    }

    @Override
    protected void initView() {
        super.initView();
        FV();

    }

    @Override
    protected void initData() {
        super.initData();

        phone = getIntent().getStringExtra("phone");
        phoneCode = getIntent().getStringExtra("phoneCode");



    }

    private void FV() {
        top_left_iv=findViewById(R.id.top_left_iv);

        top_left_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        title_tv=findViewById(R.id.m_title_tv);
        rightTitle_tv=findViewById(R.id.m_righttitle_tv);
        ViewUtils.displayView(title_tv);
        ViewUtils.hideView(rightTitle_tv);
        ViewUtils.setTextViewStr(title_tv,"找回密码");

        p1 = findViewById(R.id.cb1_ed);
        p2 = findViewById(R.id.cb2_ed);

    }

    //完成
    public void complete(View v){
        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }

        final String psw1 = p1.getText().toString().trim();
        String psw2 = p2.getText().toString().trim();
        if (TextUtils.isEmpty(psw1)){
            ToastUtil.show("密码不能为空");
            return;
        }
        if (psw1.length()>20){
            ToastUtil.show("密码长度不能超过20位");
            return;
        }
        if (!TextUtils.equals(psw1,psw2)){
            ToastUtil.show("两次密码输入不一致");
            return;
        }



        NbNetUtils.getInstance().modifyPsw(this, phone, psw1, phoneCode, new HttpResponse2<NbBaseResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestResultDesc, int requestResultCode, NbBaseResponse response, String s) {
                LogUtils.i(TAG,"---modifyPsw = " + s);

                if (isRequestSuccess){
                    if (requestResultCode==1){
                        //说明用户密码修改成功
                        ToastUtil.show("密码修改成功");
                        ActivityManagers.getInstance().closeActivity(FindPswActivity1.TAG);
                        finish();
                    }else if (requestResultCode==0){
                        ToastUtil.show("验证码错误");
                    }


                }else {
                    ToastUtil.show("密码修改失败");
                }


            }
        });




    }
}
